package com.we823.project.repository.service.product;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.we823.project.common.web.status.TaskStatus;
import com.we823.project.model.product.ProductTaskModel;
import com.we823.project.repository.dao.product.ProductTaskRepository;
import com.we823.project.repository.service.AbstractBaseService;

@Service("productTaskService")
public class ProductTaskService extends AbstractBaseService<ProductTaskModel> {

	private ProductTaskRepository productTaskRepository;
	
	@Autowired
	public ProductTaskService(ProductTaskRepository productTaskRepository) {
		super(productTaskRepository);
		this.productTaskRepository = productTaskRepository;
	}

	public List<ProductTaskModel> findByStatusAndUserId(TaskStatus status, int userId) {
		return productTaskRepository.findByStatusAndUserId(status,userId);
	}

	public List<ProductTaskModel> findByUserIdAndStatusIn(int userId, List<TaskStatus> statusList) {
		return productTaskRepository.findByUserIdAndStatusIn(userId,statusList);
	}

	public Page<ProductTaskModel> findByStatusAndUserIdAndDate(
			TaskStatus taskStatus, int userId, int year, int month, int day,
			Pageable pageable) {
		if (day>0) {
			return productTaskRepository.findByStatusAndUserIdAndyFinishtime(taskStatus,userId,year,month,day,pageable);
		}else if(month>0) {
			return productTaskRepository.findByStatusAndUserIdAndyFinishtime(taskStatus,userId,year,month,pageable);
		}else if(year>0) {
			return productTaskRepository.findByStatusAndUserIdAndyFinishtime(taskStatus,userId,year,pageable);
		}else {
			return productTaskRepository.findByStatusAndUserId(taskStatus,userId,pageable);
		}
	}

	public List<ProductTaskModel> findByUserIdAndStatusIn(int userId,
			List<TaskStatus> statusList, Sort sort) {
		return productTaskRepository.findByUserIdAndStatusIn(userId, statusList,sort);
	}

	public List<ProductTaskModel> findByStatusAndUserId(TaskStatus status,
			int userId, Sort sort) {
		return productTaskRepository.findByStatusAndUserId(status, userId ,sort);
	}

	public List<ProductTaskModel> findByIdIn(Set<Integer> idSet){
		return productTaskRepository.findByIdIn(idSet);
	}
	public Map<Integer, ProductTaskModel> mapByIds(Set<Integer> taskIdSet) {
		List<ProductTaskModel> productTaskModels = findByIdIn(taskIdSet);
		if (productTaskModels!=null && productTaskModels.size()>0) {
			Map<Integer, ProductTaskModel> map = Maps.newHashMap();
			for (ProductTaskModel productTaskModel : productTaskModels) {
				map.put(productTaskModel.getId(), productTaskModel);
			}
			return map;
		}
		return null;
	}

	public Page<ProductTaskModel> findByUserId(int userId,
			PageRequest pageRequest) {
		if (pageRequest==null) {
			return new PageImpl<ProductTaskModel>(productTaskRepository.findByUserId(userId));
		}
		return productTaskRepository.findByUserId(userId,pageRequest);
	}

	public Page<ProductTaskModel> findByUserIdAndAddtime(int userId, int year,
			int month, int day, Pageable pageable) {
		if (pageable!=null) {
			if (day>0) {
				return productTaskRepository.findByUserIdAndAddtime(userId,year,month,day,pageable);
			}else if(month>0) {
				return productTaskRepository.findByUserIdAndAddtime(userId,year,month,pageable);
			}else if (year>0) {
				return productTaskRepository.findByUserIdAndAddtime(userId,year,pageable);
			}else {
				return productTaskRepository.findByUserId(userId, pageable);
			}
		}else {
			if (day>0) {
				return new PageImpl<ProductTaskModel>(productTaskRepository.findByUserIdAndAddtime(userId,year,month,day));
			}else if(month>0) {
				return new PageImpl<ProductTaskModel>(productTaskRepository.findByUserIdAndAddtime(userId,year,month));
			}else if (year>0) {
				return new PageImpl<ProductTaskModel>(productTaskRepository.findByUserIdAndAddtime(userId,year));
			}else {
				return new PageImpl<ProductTaskModel>(productTaskRepository.findByUserId(userId));
			}
		}
		
		
	}

	public List<ProductTaskModel> findByUserId(Integer userId) {
		return productTaskRepository.findByUserId(userId);
	}

	public List<ProductTaskModel> findByPlanDetialIdIn(
			List<Integer> planDetailIdList) {
		return productTaskRepository.findByPlanDetailIdIn(planDetailIdList);
	}

	public ProductTaskModel findByPlanDetialId(Integer planDetailId) {
		return productTaskRepository.findByPlanDetailId(planDetailId);
	}

	public Page<ProductTaskModel> findByUserIdAndAddtimeBetween(int userId,
			Date fromDate, Date endDate, Pageable pageable) {
		if (pageable==null) {
			return new PageImpl<ProductTaskModel>(productTaskRepository.findByUserIdAndAddtimeBetween(userId, fromDate, endDate));
		}
		return productTaskRepository.findByUserIdAndAddtimeBetween(userId,fromDate,endDate,pageable);
	}

}
